/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangePermissionDlg.java
 *
 * Created on May 5, 2010, 9:20:20 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.ManagementBiz;
import javax.swing.JOptionPane;

/**
 *
 * @author Tai Nguyen
 */
public class ChangePermissionDlg extends javax.swing.JDialog {

    ManagementBiz manaBuz = new ManagementBiz();

    public ChangePermissionDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    /**
     *
     * @return int value
     */
    public int valid() {
        int permissID = -1;
        if (rdbUser.isSelected()) {
            permissID = 1;
        } else if (rdbProduct.isSelected()) {
            permissID = 2;
        } else if (rdbCampaign.isSelected()) {
            permissID = 3;
        } else if (rdbProCam.isSelected()) {
            permissID = 4;
        } else if (rdbAdmin.isSelected()) {
            permissID = 5;
        }
        return permissID;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroupPermission = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        lblPermission = new javax.swing.JLabel();
        rdbUser = new javax.swing.JRadioButton();
        rdbAdmin = new javax.swing.JRadioButton();
        rdbProduct = new javax.swing.JRadioButton();
        rdbProCam = new javax.swing.JRadioButton();
        rdbCampaign = new javax.swing.JRadioButton();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblPresent = new javax.swing.JLabel();
        lblUser = new javax.swing.JLabel();
        lblPresentPermiss = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Change Permission");
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setMinimumSize(new java.awt.Dimension(200, 30));
        jPanel1.setPreferredSize(new java.awt.Dimension(200, 30));
        jPanel1.setLayout(new java.awt.BorderLayout());

        lblTitle.setBackground(new java.awt.Color(51, 102, 255));
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        lblTitle.setForeground(new java.awt.Color(51, 102, 255));
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/CHANGEPERMISSIONBANNER.jpg"))); // NOI18N
        jPanel1.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        getContentPane().add(jPanel1, gridBagConstraints);

        lblUserName.setText("UserName : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 12, 0);
        getContentPane().add(lblUserName, gridBagConstraints);

        lblPermission.setText("Permission : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 12, 0);
        getContentPane().add(lblPermission, gridBagConstraints);

        btnGroupPermission.add(rdbUser);
        rdbUser.setText("User");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 18, 10, 0);
        getContentPane().add(rdbUser, gridBagConstraints);

        btnGroupPermission.add(rdbAdmin);
        rdbAdmin.setText("Admministrator");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 18, 21, 0);
        getContentPane().add(rdbAdmin, gridBagConstraints);

        btnGroupPermission.add(rdbProduct);
        rdbProduct.setText("Product Master");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 18, 10, 0);
        getContentPane().add(rdbProduct, gridBagConstraints);

        btnGroupPermission.add(rdbProCam);
        rdbProCam.setText("P&C Master");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 18, 10, 0);
        getContentPane().add(rdbProCam, gridBagConstraints);

        btnGroupPermission.add(rdbCampaign);
        rdbCampaign.setText("Campaign Master");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 18, 10, 0);
        getContentPane().add(rdbCampaign, gridBagConstraints);

        btnSave.setBackground(new java.awt.Color(51, 102, 255));
        btnSave.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnSave.setForeground(new java.awt.Color(255, 255, 255));
        btnSave.setText("Save Change");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        getContentPane().add(btnSave, gridBagConstraints);

        btnCancel.setBackground(new java.awt.Color(51, 102, 255));
        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnCancel.setForeground(new java.awt.Color(255, 255, 255));
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
        getContentPane().add(btnCancel, gridBagConstraints);

        lblPresent.setText("Present Permission : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 13, 0);
        getContentPane().add(lblPresent, gridBagConstraints);

        lblUser.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblUser.setForeground(new java.awt.Color(0, 0, 153));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 18, 0, 0);
        getContentPane().add(lblUser, gridBagConstraints);

        lblPresentPermiss.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblPresentPermiss.setForeground(new java.awt.Color(0, 0, 153));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 18, 13, 0);
        getContentPane().add(lblPresentPermiss, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        int permissID = valid();
        String permissionName = null;
        if (permissID != -1) {
            if (permissID == 1) {
                permissionName = "USER";
            } else if (permissID == 2) {
                permissionName = "PRODUCT MASTER";
            } else if (permissID == 3) {
                permissionName = "CAMPAIGN MASTER";
            } else if (permissID == 4) {
                permissionName = "PRODUCT & CAMPAIGN MASTER";
            } else if (permissID == 5) {
                permissionName = "ADMIN";
            }
            //
            if (manaBuz.changePermission(lblUser.getText(), permissID, permissionName)) {
                JOptionPane.showMessageDialog(this, manaBuz.getLasterror());
                dispose();
            }
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChangePermissionDlg dialog = new ChangePermissionDlg(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.ButtonGroup btnGroupPermission;
    private javax.swing.JButton btnSave;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblPermission;
    private javax.swing.JLabel lblPresent;
    public javax.swing.JLabel lblPresentPermiss;
    private javax.swing.JLabel lblTitle;
    public javax.swing.JLabel lblUser;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JRadioButton rdbAdmin;
    private javax.swing.JRadioButton rdbCampaign;
    private javax.swing.JRadioButton rdbProCam;
    private javax.swing.JRadioButton rdbProduct;
    private javax.swing.JRadioButton rdbUser;
    // End of variables declaration//GEN-END:variables
}
